package cn.com.xjnt.vas.web.tags;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.google.common.collect.Lists;

public class SelectTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;
	
	private String id;
	
	private String name;
	
	private String className;
		
	private String var;
	
	private Iterator<?> items;
	
	@Override
	public int doStartTag() throws JspException {
		try {
			if(items.hasNext()){
				pageContext.setAttribute(var, items.next());
			}
			StringBuilder strBuilder = new StringBuilder();
			strBuilder.append("<select id='").append(id).append("' ");
			strBuilder.append("name='").append(name).append("' ");
			strBuilder.append("class='").append(className).append("'>");
			pageContext.getOut().write(strBuilder.toString());
		} catch (IOException e) {
			throw new JspTagException("发生错误!", e);
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		if(items.hasNext()){
			pageContext.setAttribute(var, items.next());
			return EVAL_BODY_AGAIN;
		}
		return SKIP_BODY;
	}
	
	@Override
	public int doEndTag() throws JspException {
		try {
			pageContext.removeAttribute(var);
			StringBuilder strBuilder = new StringBuilder();
			strBuilder.append("</select>");
			pageContext.getOut().write(strBuilder.toString());
		} catch (IOException e) {
			throw new JspTagException("发生错误!", e);
		}
		return EVAL_PAGE;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public void setVar(String var) {
		this.var = var;
	}
	
	public void setItems(Object items) {
		if (items instanceof Collection<?>){
			this.items = ((Collection<?>) items).iterator();
		} else {
			this.items = Lists.newArrayList().iterator();
		}
	}
}
